var gl = null;

	var aLocation;
	var aTextureLocation;
	var shaderProgram;

function Mopengl()
{
	//
}

Mopengl.initWebGl = function()
{
	Mopengl.canvas = document.getElementById("canvas");

	try
	{
		gl = Mopengl.canvas.getContext("experimental-webgl");
	} catch(e)
	{
		alert(e);
	}

	if(gl)
	{

		gl.clearColor(0.0, 0.0, 0.0, 1.0);
		gl.clearDepth(1.0);
		gl.enable(gl.DEPTH_TEST);
		//gl.depthFunc(gl.LEQUAL);
		gl.depthFunc(gl.LESS);

		gl.enable(gl.BLEND);
		gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);

	} else
	{
		alert("Error while initializing webgl\n");
	}
}

Mopengl.initShaders = function()
{
	var vtx_shader = Mopengl.getShader(gl, "vtx_shader");
	var fgt_shader = Mopengl.getShader(gl, "fgt_shader");

	shaderProgram = gl.createProgram();
	gl.attachShader(shaderProgram, vtx_shader);
	gl.attachShader(shaderProgram, fgt_shader);
	gl.linkProgram(shaderProgram);

	if(!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)){
		alert("Error while linking the program\n");
	}

	gl.useProgram(shaderProgram);

	aLocation = gl.getAttribLocation(shaderProgram, "currentVertice");
	gl.enableVertexAttribArray(aLocation);

	aTextureLocation = gl.getAttribLocation(shaderProgram, "currentTexCoord")
	gl.enableVertexAttribArray(aTextureLocation);
}

Mopengl.initDevice = function()
{
	Mopengl.mouse = new Mouse(Mopengl.canvas);
}


Mopengl.getShader = function(gl, id)
{
	var xshader = document.getElementById(id);
	if(!xshader)
	{
		alert("xshader not found\n");
		return null;
	}


	var tshader = "";
	var currentChild = xshader.firstChild;

	while(currentChild)
	{
		if(currentChild.nodeType == 3)
			tshader += currentChild.textContent;

		currentChild = currentChild.nextSibling;
	}
				
	if(tshader == "")
	{
		alert("is shader empty ?\n");
		return null;
	}


	var cshader = null;

	if(xshader.type == "xshader/x-fragment")
		cshader = gl.createShader(gl.FRAGMENT_SHADER);
	else if(xshader.type == "xshader/x-vertex" )
		cshader = gl.createShader(gl.VERTEX_SHADER);
	else
	{
		alert("xshader type not found\n");
		return null;
	}


	if(!cshader)
		alert("compiled shader is null");

	gl.shaderSource(cshader, tshader);
	gl.compileShader(cshader);

	if(!gl.getShaderParameter(cshader, gl.COMPILE_STATUS))
	{
		alert(id + ": Error while compiling the shader\n" +
		gl.getShaderInfoLog(cshader));
		return null;
	}

	return cshader;
}
